package com.android.base.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;


public abstract class BaseSimpleAdapter<Data, DB extends ViewDataBinding> extends RecyclerView.Adapter<BaseSimpleAdapter<Data, DB>.BaseSimpleViewHolder> {

    private List<Data> data;
    private int itemId;
    protected Context context;
    private int variableId;
    private OnItemClickListener<Data> listener;

    public interface OnItemClickListener<Data> {
        void onItemClick(Data data, int position);

        boolean onItemLongClick(Data data, int position);
    }


    public BaseSimpleAdapter(@LayoutRes int itemId, int variableId) {
        this.itemId = itemId;
        this.variableId = variableId;
        data = new ArrayList<>();
    }

    public BaseSimpleAdapter(List<Data> data, @LayoutRes int itemId, int variableId) {
        this.data = data == null ? new ArrayList<>() : data;
        this.itemId = itemId;
        this.variableId = variableId;
    }


    @NonNull
    @Override
    public BaseSimpleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
        this.context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        DB binding = DataBindingUtil.inflate(inflater, itemId, parent, false);
        return new BaseSimpleViewHolder(binding.getRoot());
    }

    @Override
    public void onBindViewHolder(BaseSimpleViewHolder holder, final int position) {
        DB binding = DataBindingUtil.getBinding(holder.itemView);
        final Data itemData = data.get(position);
        binding.setVariable(variableId, itemData);
        onBindViewHolder(itemData, binding, position);

        //设置点击事件
        if (listener != null) {
            holder.itemView.setOnClickListener(v -> listener.onItemClick(itemData, position));
            holder.itemView.setOnLongClickListener(v -> listener.onItemLongClick(itemData, position));
        }
        //立即绑定
        binding.executePendingBindings();
    }


    @Override
    public int getItemCount() {
        return data.size();
    }

    private void onBindViewHolder(Data data, DB binding, int position) {}

    public void setNewData(List<Data> data) {
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void addData(Data data) {
        this.data.add(data);
        notifyDataSetChanged();
    }


    public void addData(List<Data> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void setOnItemListener(OnItemClickListener<Data> listener) {
        this.listener = listener;
    }

    class BaseSimpleViewHolder extends RecyclerView.ViewHolder {
        BaseSimpleViewHolder(View itemView) {
            super(itemView);
        }
    }
}


/***
public class NewsAdapter extends BaseDBRVAdapter<NewsBean.StoriesBean, ItemNesBinding> {

    public NewsAdapter() {
        super(R.layout.item_nes, BR.bean);
    }
    @BindingAdapter({"imageUrl"})
    public static void loadImage(ImageView imageView, String url) {
        Glide.with(imageView.getContext()).load(url).into(imageView);
    }
}*/
